package Jets.ui
{
   import flash.display.DisplayObjectContainer;
   import flash.events.Event;
   import flash.events.MouseEvent;
   import flash.geom.Rectangle;
   import Jets.display.JContainer;
   
   public class JScrollBar extends JContainer
   {
	   public static const HORIZONTAL:String = "horizontal"; 
	   public static const VERTICAL:String = "vertical";
	   public static var DefaultWidth:int = 10;
	   public static var DefaultHeight:int = 0;
	   
	   public var _handle:JPushButton;
	   private var _back:JPanel;
	  
	   private var _backClick:Boolean = true;
	   
	   private var _barheight:Number = 35;
	   
	   protected var _value:Number = 0;
	   protected var _max:Number = 100;
	   protected var _min:Number = 0;

	   protected var _orientation:String;
	   
	   private var _arrowheight:int = 10;
	   private var _arrowwidth:int = 0;
	   protected var uparrow:JPushButton;
	   protected var downarrow:JPushButton;
	   
      public function JScrollBar(type:String="horizontal", parent:DisplayObjectContainer=null, cx:Number=0, cy:Number=0, change:Function=null) 
	  {
         this._orientation = type;
         super(parent,cx,cy);
         if(change != null)
         {
            addEventListener(Event.CHANGE,change);
         }
      }
      
      public static function setArrowSize(cw:int=12, ch:int=0) : void 
	  {
         DefaultWidth = cw;
         DefaultHeight = ch;
      }
      
     /**
	 *设置箭头的宽度
	 */  
      public function get arrowwidth() : int 
	  {
         return this._arrowwidth;
      }
      public function set arrowwidth(value:int) : void 
	  {
         this._arrowwidth = value;
         invalidate();
      }
      
	  /**
	   *设置箭头的高度
	   */  
      public function get arrowheight() : int 
	  {
         return this._arrowheight;
      }
      public function set arrowheight(value:int) : void 
	  {
         this._arrowheight = value;
         invalidate();
      }
      
      public function get barheight() : Number 
	  {
         return this._barheight;
      }
      public function set barheight(param1:Number) : void 
	  {
         if(this._barheight != param1)
         {
            this._barheight = param1;
            this.drawHandle();
         }
      }
      
      override protected function init() : void 
	  {
         super.init();
         if(this._orientation == HORIZONTAL)
         {
            setSize(100,this.arrowwidth);
         }
         else
         {
            setSize(this.arrowwidth,100);
         }
      }
      
      
      
      override protected function addChildren() : void 
	  {
         this._back = new JPanel(this);
         this._handle = new JPushButton();
         this._handle.addEventListener(MouseEvent.MOUSE_DOWN,this.onDrag);
         this._handle.buttonMode = true;
         this._handle.useHandCursor = true;
         addChild(this._handle);
         this.uparrow = new JPushButton();
         this.downarrow = new JPushButton();
         this.uparrow.click = this.onup;
         this.downarrow.click = this.onup;
         this.setArrow(DefaultWidth,DefaultHeight);
      }
      
      protected var mGetDeltaFunc:Function;
      
      public function set getDeltaFunc(param1:Function) : void {
         this.mGetDeltaFunc = param1;
      }
      
      private function onup(param1:MouseEvent) : void {
         var _loc2_:Number = 10;
         if(null != this.mGetDeltaFunc)
         {
            _loc2_ = this.mGetDeltaFunc(this._min,this._max);
         }
         if(this._orientation == HORIZONTAL)
         {
            if(param1.currentTarget == this.uparrow)
            {
               this.value = this.value - _loc2_;
            }
            else
            {
               if(param1.currentTarget == this.downarrow)
               {
                  this.value = this.value + _loc2_;
               }
            }
         }
         else
         {
            if(param1.currentTarget == this.uparrow)
            {
               this.value = this.value + _loc2_;
            }
            else
            {
               if(param1.currentTarget == this.downarrow)
               {
                  this.value = this.value - _loc2_;
               }
            }
         }
         this.dispatchEvent(new Event(Event.CHANGE));
      }
      
      public function setArrow(param1:int, param2:int) : void {
         this._arrowwidth = param1;
         this._arrowheight = param2;
         if(this.arrowheight > 0)
         {
            this.addChild(this.uparrow);
            this.addChild(this.downarrow);
            this.uparrow.setSize(this.arrowwidth,this.arrowheight);
            this.downarrow.setSize(this.arrowwidth,this.arrowheight);
         }
         else
         {
            if(this.contains(this.uparrow))
            {
               this.removeChild(this.uparrow);
            }
            if(this.contains(this.downarrow))
            {
               this.removeChild(this.downarrow);
            }
         }
      }
      
      protected function drawBack() : void {
         this._back.setSize(_width,_height - this.arrowheight * 2);
         this._back.y = this.arrowheight;
         if(this._backClick)
         {
            this._back.addEventListener(MouseEvent.MOUSE_DOWN,this.onBackClick);
         }
         else
         {
            this._back.removeEventListener(MouseEvent.MOUSE_DOWN,this.onBackClick);
         }
      }
      
      protected function drawHandle() : void {
         if(this._orientation == HORIZONTAL)
         {
            this._handle.setSize(this._barheight,_height);
         }
         else
         {
            this._handle.setSize(_width,this._barheight);
         }
         this._handle.invalidateNow();
         this.positionHandle();
      }
      
      private function correctValue() : void {
         if(this._max > this._min)
         {
            this._value = Math.min(this._value,this._max);
            this._value = Math.max(this._value,this._min);
         }
         else
         {
            this._value = Math.max(this._value,this._max);
            this._value = Math.min(this._value,this._min);
         }
      }
      
      protected function positionHandle() : void {
         var _loc1_:* = NaN;
         if(this._orientation == HORIZONTAL)
         {
            _loc1_ = width - this._handle.width - this.arrowwidth * 2;
            this._handle.x = this.arrowwidth + (this._value - this._min) / (this._max - this._min) * _loc1_;
         }
         else
         {
            _loc1_ = height - this._handle.height - this.arrowheight * 2;
            this._handle.y = this.arrowheight + _loc1_ - (this._value - this._min) / (this._max - this._min) * _loc1_;
         }
      }
      
      override public function draw() : void {
         this.drawBack();
         this.drawHandle();
      }
      
      public function setSliderParams(param1:Number, param2:Number, param3:Number) : void {
         this.minimum = param1;
         this.maximum = param2;
         this.value = param3;
      }
      
      private function onBackClick(param1:MouseEvent) : void {
         var _loc2_:* = 0;
         var _loc3_:* = 0;
         if(this._orientation == HORIZONTAL)
         {
            this._handle.x = mouseX - height / 2;
            this._handle.x = Math.max(this._handle.x,this.arrowwidth);
            this._handle.x = Math.min(this._handle.x,width - this._handle.width - this.arrowwidth);
            _loc2_ = _width - this.arrowwidth * 2 - this.barheight;
            _loc3_ = this._handle.x - this.arrowwidth;
            this._value = _loc3_ / _loc2_ * (this._max - this._min) + this._min;
         }
         else
         {
            this._handle.y = mouseY - width / 2;
            this._handle.y = Math.max(this._handle.y,this.arrowheight);
            this._handle.y = Math.min(this._handle.y,height - this._handle.height - this.arrowheight);
            this._value = (_height - this._handle.height - this._handle.y) / (height - this._handle.height) * (this._max - this._min) + this._min;
         }
         dispatchEvent(new Event(Event.CHANGE));
      }
      
      protected function onDrag(param1:MouseEvent) : void {
         stage.addEventListener(MouseEvent.MOUSE_UP,this.onDrop);
         stage.addEventListener(MouseEvent.MOUSE_MOVE,this.onSlide);
         if(this._orientation == HORIZONTAL)
         {
            this._handle.startDrag(false,new Rectangle(this.arrowwidth,0,width - this._handle.width - this.arrowwidth * 2,0));
         }
         else
         {
            this._handle.startDrag(false,new Rectangle(0,this.arrowheight,0,height - this._handle.height - this.arrowheight * 2));
         }
      }
      
      protected function onDrop(param1:MouseEvent) : void {
         if(param1.target.stage)
         {
            param1.target.stage.removeEventListener(MouseEvent.MOUSE_UP,this.onDrop);
            param1.target.stage.removeEventListener(MouseEvent.MOUSE_MOVE,this.onSlide);
         }
         stopDrag();
      }
      
      protected function onSlide(param1:MouseEvent) : void {
         var _loc3_:* = 0;
         var _loc4_:* = 0;
         var _loc5_:* = 0;
         var _loc6_:* = 0;
         var _loc2_:Number = this._value;
         if(this._orientation == HORIZONTAL)
         {
            _loc3_ = _width - this.arrowwidth * 2 - this.barheight;
            _loc4_ = this._handle.x - this.arrowwidth;
            this._value = _loc4_ / _loc3_ * (this._max - this._min) + this._min;
         }
         else
         {
            _loc5_ = _height - this.arrowheight * 2;
            _loc6_ = this._handle.y - this.arrowheight;
            this._value = (_loc5_ - this._handle.height - _loc6_) / (_loc5_ - this._handle.height) * (this._max - this._min) + this._min;
         }
         if(this._value != _loc2_)
         {
            dispatchEvent(new Event(Event.CHANGE));
         }
      }
      
      public function set handleskin(param1:String) : void {
         this._handle.skin = param1;
      }
      
      public function get handleskin() : String {
         return this._handle.skin;
      }
      
      override public function set skin(param1:String) : void {
         this._back.skin = param1;
      }
      
      override public function get skin() : String {
         return this._back.skin;
      }
      
      public function set backClick(param1:Boolean) : void {
         this._backClick = param1;
         invalidate();
      }
      
      public function get backClick() : Boolean 
	  {
         return this._backClick;
      }
      
      public function set value(value:Number) : void 
	  {
		 value = value * 10 / 10;
         if(this._value != value)
         {
            this._value = value;
            this.correctValue();
            this.positionHandle();
            this.dispatchEvent(new Event(Event.SCROLL));
         }
      }
      
      public function get value() : Number 
	  {
         return this._value;
      }
      
      public function set maximum(value:Number) : void 
	  {
         this._max = value;
         this.correctValue();
         this.positionHandle();
      }
      
      public function get maximum() : Number 
	  {
         return this._max;
      }
      
      public function set minimum(value:Number) : void 
	  {
         this._min = value;
         this.correctValue();
         this.positionHandle();
      }
      
      public function get minimum() : Number 
	  {
         return this._min;
      }
   }
}
